#include "Image.h"
#include "SDL/SDL_image.h"
#include "Window.h"
#include <iostream>
using namespace fe;

Image::Image(const std::string &name)
:
    m_alpha(1.f),
    m_width(0), m_height(0)
{
    m_rotX = 0.f;
    m_rotY = 0.f;
    load(name);
}

Image::Image()
:
    m_alpha(1.f),
    m_width(0), m_height(0)
{
    m_rotX = 0.f;
    m_rotY = 0.f;
    m_location.x = 0;
    m_location.y = 0;
}

Image::~Image()
{
    if(glIsTexture(m_texture))
        glDeleteTextures(1, &m_texture);
}

void Image::rotate(float x, float y)
{
    m_rotX = x;
    m_rotY = y;
}

void Image::load(const std::string &filename)
{
    m_filename = filename;

    glEnable(GL_TEXTURE_2D);

    SDL_Surface* Surface = NULL;

    SDL_Surface *TMP = IMG_Load(filename.c_str());
    if(TMP == NULL) {
        std::cout << "image not loaded" << std::endl;
    }


    SDL_SetColorKey(TMP,SDL_SRCCOLORKEY,SDL_MapRGB(TMP->format,0xff,0x00,0xff));

    Surface = SDL_DisplayFormatAlpha(TMP);

    SDL_FreeSurface(TMP);

    glGenTextures(1,&m_texture);

    glBindTexture(GL_TEXTURE_2D, m_texture);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

    if(Surface->format->Rmask == 0x000000ff)
    {
        glTexImage2D(GL_TEXTURE_2D,0,4,Surface->w,Surface->h,0,GL_RGBA,GL_UNSIGNED_BYTE,Surface->pixels);
    }
    else
    {
        glTexImage2D(GL_TEXTURE_2D,0,4,Surface->w,Surface->h,0,GL_BGRA,GL_UNSIGNED_BYTE,Surface->pixels);
    }


    m_clip[0].x = 0;
    m_clip[0].y = 0 ;
    m_clip[1].x = 1;
    m_clip[1].y = 0;
    m_clip[2].x = 1 ;
    m_clip[2].y = 1;
    m_clip[3].x = 0;
    m_clip[3].y = 1;



    m_position[0].x = 0;
    m_position[0].y = 0 ;
    m_position[1].x = Surface->w;
    m_position[1].y = 0;
    m_position[2].x = Surface->w ;
    m_position[2].y = Surface->h;
    m_position[3].x = 0;
    m_position[3].y = Surface->h;


    m_location.x = 0;
    m_location.y = 0;

    m_width = Surface->w;
    m_height = Surface->h;

    m_imageWidth = Surface->w;
    m_imageHeight = Surface->h;

   // update();


    m_alpha = 1.f;

    SDL_FreeSurface(Surface);
}

void Image::render(Window& window)
{
    glPushMatrix();
        glTranslatef(m_location.x, m_location.y, 0.f);
        //glRotate2f(m_rotX, m_rotY, 0.f);
        glDisable(GL_COLOR);
        glColor4f(1.f, 1.f, 1.f, m_alpha);

        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, m_texture);

        glVertexPointer(2, GL_FLOAT, 0, (GLvoid*)&m_position);
        glTexCoordPointer(2, GL_FLOAT, 0, (GLvoid*)&m_clip);

        glDrawArrays(GL_QUADS, 0, 4);
    glPopMatrix();
}


void Image::setPosition(float x, float y)
{
    m_location.x = x;
    m_location.y = y;
    //update();
}

void Image::move(float offx, float offy)
{
    m_location.x += offx;
    m_location.y += offy;
    //update();
}

void Image::setAlpha(float alpha)
{
    m_alpha = alpha;
}

void Image::setClip(float x, float y, float w, float h)
{

    m_width = w;
    m_height = h;
    m_clip[0].x = x/m_imageWidth;
    m_clip[0].y = y/m_imageHeight;

    m_clip[1].x = (x+w)/m_imageWidth;
    m_clip[1].y = (y+h)/m_imageHeight;

    m_clip[2].x = (x+w)/m_imageWidth;
    m_clip[2].y = (y+h)/m_imageHeight;

    m_clip[3].x = x/m_imageWidth;
    m_clip[3].y = (y+h)/m_imageHeight;

    update();
}


void Image::update()
{
    m_position[0] = m_location;

    m_position[1].x = m_location.x + m_width;
    m_position[1].y = m_location.y;

    m_position[2].x = m_location.x + m_width;
    m_position[2].y = m_location.y + m_height;

    m_position[3].x = m_location.x;
    m_position[3].y = m_location.y + m_height;
}
